package com.mhs.tracuu;

import java.util.ArrayList;

import com.mhs.BUS.PhanLoaiThuocBUS;
import com.mhs.BUS.ThuocBUS;
import com.mhs.DTO.PhanLoaiThuocDTO;
import com.mhs.DTO.ThuocDTO;
import com.mhs.moreadapter.ExpandableListView_Adapter;
import com.mhs.moreadapter.ExpandableListView_Category;
import com.mhs.moreadapter.ExpandableListView_Item;
import com.mhs.mydialog.MyDialogListner;

import com.mhs.mhs_gui.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ExpandableListView.OnChildClickListener;

public class TraCuuThuoc_Search_Detail extends Activity
{
	ExpandableListView_Adapter adapter;
	public final Context THIS = this;

	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tracuuthuoc_search_detail);
		
		int maThuoc = 0;
		Bundle extras = getIntent().getExtras();
		if(extras !=null) {
			maThuoc = extras.getInt("maThuoc");
		}

		ExpandableListView listView = (ExpandableListView) findViewById(R.id.elv_loaitimkiem);

		listView.setOnChildClickListener(new OnChildClickListener() 
		{
			public boolean onChildClick(ExpandableListView parent, View view, int groupPosition, int childPosition, long id){
				return false;
			}
		});

		ExportData(maThuoc);
		listView.setAdapter(adapter);
	}


	public void ExportData(int maThuoc){
		ThuocBUS thuocBUS = new ThuocBUS();
		ThuocDTO thuocDTO = thuocBUS.getThuocFromMaThuoc(this, maThuoc);
		if(thuocDTO == null){
			// do nothing
			MyDialogListner.ShowMessageAlert(this, "Có lỗi xảy ra, vui lòng thử lại!");
			return;
		}else{
			TextView tv_tenthuoc = (TextView)findViewById(R.id.tv_tenthuoc);
			tv_tenthuoc.setText(thuocDTO.getTENTHUOC());
			
			TextView tv_dangbaoche = (TextView)findViewById(R.id.tv_dangbaoche);
			tv_dangbaoche.setText(thuocDTO.getDANGBAOCHE());
			
			TextView tv_quycachdonggoi = (TextView)findViewById(R.id.tv_quycachdonggoi);
			tv_quycachdonggoi.setText(thuocDTO.getDONGGOI());
			
			TextView tv_phanloaithuoc = (TextView)findViewById(R.id.tv_phanloaithuoc);
			PhanLoaiThuocBUS pltBUS = new PhanLoaiThuocBUS();
			PhanLoaiThuocDTO pltDTO = pltBUS.getPhanLoaiThuocFromMaLoaiThuoc(this, thuocDTO.getMALOAITHUOC());
			if(pltDTO != null){
				tv_phanloaithuoc.setText("Nhóm: " + pltDTO.getTENLOAITHUOC());
			}
			
			TextView tv_thanhphan = (TextView)findViewById(R.id.tv_thanhphan);
			tv_thanhphan.setText("Thành phần: " + thuocDTO.getTHANHPHAN());
			
			ArrayList<ExpandableListView_Category>  groups = new ArrayList<ExpandableListView_Category> ();

			ExpandableListView_Category category = new ExpandableListView_Category();
			category.setId(1);
			category.setName("Chỉ định");
			groups.add(category);
			
			category = new ExpandableListView_Category();
			category.setId(2);
			category.setName("Chống chỉ định");
			groups.add(category);
			
			category = new ExpandableListView_Category();
			category.setId(3);
			category.setName("Tác dụng phụ");
			groups.add(category);
			
			category = new ExpandableListView_Category();
			category.setId(4);
			category.setName("Tương tác thuốc");
			groups.add(category);
			
			category = new ExpandableListView_Category();
			category.setId(5);
			category.setName("Chú ý đề phòng");
			groups.add(category);
			
			category = new ExpandableListView_Category();
			category.setId(6);
			category.setName("Liều lượng");
			groups.add(category);
			
			category = new ExpandableListView_Category();
			category.setId(7);
			category.setName("Thông tin khác");
			groups.add(category);

			ArrayList<ArrayList<ExpandableListView_Item>> children = new ArrayList<ArrayList<ExpandableListView_Item>>();
			ArrayList<ExpandableListView_Item> itemList;
	
			//Dùng cho chỉ định
			ExpandableListView_Item item1 = new ExpandableListView_Item();
			item1.setCateId(1);
			item1.setItemId(0);
			item1.setName(thuocDTO.getCHIDINH());
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item1);
			children.add(itemList);

			
			//Dùng cho chống chỉ định
			ExpandableListView_Item item2 = new ExpandableListView_Item();
			item2.setCateId(2);
			item2.setItemId(0);
			item2.setName(thuocDTO.getCHONGCHIDINH());
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item2);
			children.add(itemList);

			//Tác dụng phụ
			ExpandableListView_Item item3 = new ExpandableListView_Item();
			item3.setCateId(3);
			item3.setItemId(0);
			item3.setName(thuocDTO.getTACDUNGPHU());
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item3);
			children.add(itemList);
			
			// tuong tac thuoc
			ExpandableListView_Item item4 = new ExpandableListView_Item();
			item4.setCateId(4);
			item4.setItemId(0);
			item4.setName(thuocDTO.getTUONGTACTHUOC());
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item4);
			children.add(itemList);
			
			// chu y de phong
			ExpandableListView_Item item5 = new ExpandableListView_Item();
			item5.setCateId(5);
			item5.setItemId(0);
			item5.setName(thuocDTO.getCHUYDEPHONG());
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item5);
			children.add(itemList);

			//Lieu luong
			ExpandableListView_Item item6 = new ExpandableListView_Item();
			item6.setCateId(6);
			item6.setItemId(0);
			item6.setName(thuocDTO.getLIEULUONG());
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item6);
			children.add(itemList);
			
			//bao quan
			ExpandableListView_Item item7 = new ExpandableListView_Item();
			item7.setCateId(7);
			item7.setItemId(0);
			item7.setName("Bảo quản: " + thuocDTO.getBAOQUAN() + "\r\n" +
							" Số đăng ký: " + thuocDTO.getSODANGKY() + "\r\n" + 
							" Công ty sản xuất: " + thuocDTO.getCONGTYSANXUAT() + "\r\n");
			itemList = new ArrayList<ExpandableListView_Item>();
			itemList.add(item7);
			children.add(itemList);
			
			adapter = new ExpandableListView_Adapter(THIS, groups, children);
		}

		// create new Item and add it directly to the adapter with the help of
		// method addItem that we have created to test it
		//try {
		//	ExpandableListView_Item item = new ExpandableListView_Item();
		//	item.setCateId(1);// add it to Bears Category
		//	item.setItemId(21);// hardcode the id, we should never do it in real world!?
		//	item.setName("I am newly added bear");
		//	adapter.addItem(item);
		//} catch (ClassNotFoundException ex) {
		//	Log.v("ADD NEW ITEM", ex.toString());
		//}

	}
	
	public void StartSearchIntent(View v)
	{
		Intent tt = new Intent(this, TraCuuThuoc_Search.class);
		startActivity(tt);
	}
}